import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { getAllRolesList } from "./user.api";
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { UploadBizType } from '/@/enums/uploadEnum';
import { useI18n } from '/@/hooks/web/useI18n';
import { DescItem } from '/@/components/Description/index';
const { t } = useI18n('');


export const inviteColumns: BasicColumn[] = [
  {
    title: t('邀请人'),
    dataIndex: 'invitorName',
    width: 200,
  },
  {
    title: t('加入成员数'),
    dataIndex: 'inviteeCount',
    width: 100,
    customRender: ({ text }) => {
      return text + '人'
    },
  },
  {
    title: t('创建时间'),
    dataIndex: 'createdAt',
    width: 150,
    align: 'center',
  },
  {
    title: t('状态'),
    dataIndex: 'endDate',
    slot: 'status',
    align: 'left',
    width: 200,
  },
];


export const inviteUserDetailSchema: DescItem[] = [
  {
    label: t('邀请人'),
    field: 'invitorName'
  },
  {
    label: t('加入人数'),
    field: 'inviteeCount'
  },
  {
    label: t('短链接'),
    field: 'shortInviteLink'
  },
  {
    label: t('链接'),
    field: 'inviteLink'
  },
  {
    label: t('链接签名'),
    field: 'sign'
  },
  {
    label: t('状态'),
    field: 'isValid',
    render: (curVal, data) => {
      return render.renderTag(curVal ? '有效' : '无效', curVal ? 'green' : 'red')
    }
  },
  {
    label: t('创建时间'),
    field: 'createdAt'
  },
  {
    label: t('结束日期'),
    field: 'endDate'
  },
  {
    label: t('通知用户'),
    field: 'toUser',
    render: (curVal, data) => {
      return curVal&& curVal.join('、')
    }
  }
];


export const columns: BasicColumn[] = [
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: (record) => {
      return render.renderAvatar(record)
    },
  },
  {
    title: '用户账号',
    dataIndex: 'userName',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realName',
    width: 100,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: '角色',
    width: 140,
    customRender: ({text}) => {
      if (!text || text === '' || text.length === 0) {
        return '-'
      }
      if (text instanceof Array) {
        return text.map(e => e.roleName).join(' | ')
      }
      return text
    },
    dataIndex: 'roleList'
  },
  {
    title: '状态',
    dataIndex: 'status',
    customRender: ({text}) => {
      let color = 'green'
      if (text === '2') {
        color = 'red'
      }
      return render.renderTag(render.renderDict(text, 'user_status'), color)
    },
    width: 60,
  }
];

/**
 * 搜索用户
 */
export const searchUserFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户账号',
    component: 'Input',
    colProps: { span: 12 },
  },
  {
    field: 'test',
    label: '',
    component: 'Input',
    slot: 'addUser',
    colProps: { span: 12 },
  },

];


export const recycleColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'userName',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realName',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender:render.renderAvatar,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 80,
    sorter: true,
    customRender: ({text}) => {
      return  render.renderDict(text, 'sys_user_sex')
    }
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'account',
    componentProps: {
      placeholder: '手机号/邮箱/用户名',
    },
    component: 'JInput',
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'sys_user_sex',
      placeholder:'请选择性别',
      stringToNumber:true
    }
  },
  {
    label: '真实名称',
    field: 'realName',
    component: 'Input',
  },
  {
    label: '用户状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'user_status',
      placeholder:'请选择状态',
      stringToNumber:true
    }
  }
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
  {
    field: 'userName',
    component: 'Input',
    label: '用户账号',
    componentProps: {
    },
    dynamicDisabled: ({values}) => {
      return !!values.id;
    },
  },
  {
    label: '工号',
    field: 'workNo',
    required: false,
    component: 'Input',
    // dynamicRules: ({model,schema}) =>rules.duplicateCheckRule( "sys_user", "work_no",model,schema,true),
  },
  {
    label: '角色',
    field: 'roleIds',
    required: true,
    component: 'ApiSelect',
    componentProps: {
      mode: "multiple",
      api: getAllRolesList,
      labelField: 'roleName',
      valueField: 'id',
    },
  },
  {
    label: '所属部门',
    field: 'deptIds',
    component: 'MySelectDept',
    componentProps: {
      rowKey: 'id'
    },
  },
  {
    label: '头像',
    field: 'avatar',
    component: 'MyImageUpload',
    componentProps: {
      fileMax:1,
      bizType: UploadBizType.AVATAR
    },
  },
  {
    label: '生日',
    field: 'birthday',
    component: 'DatePicker',
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'sys_user_sex',
      placeholder:'请选择',
      // stringToNumber: true
    },
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    rules: rules.rule('email', false),
    dynamicRules: ({model, schema}) => {
      if (!model.id || (model.id && model.email?.indexOf('*') !== -1)) {
        return [ { required: false } ]
      }
      return rules.rule('email', false);
    },
  },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    dynamicRules: ({model, schema}) => {
      if (!model.phone || (model.id && (model.phone?.indexOf('*') !== -1))) {
        return [ { required: false } ]
      }
      return [
        { pattern: /^1[3|4|5|7|8|9][0-9]\d{8}$/, message: '手机号码格式有误' }
      ];
    },
  },
  {
    label: '座机',
    field: 'telephone',
    component: 'Input',
    rules: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: '请输入正确的座机号码' }]
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
      rows: 3,
    },
  }
];

export const formPasswordSchema: FormSchema[] = [
  {
    label: '用户账号',
    field: 'userName',
    component: 'Input',
    componentProps: {readonly: true}
  },
  {
    label: '',
    field: 'id',
    show: false,
    component: 'Input'
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '请输入登录密码',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({values}) => rules.confirmPassword(values,true)
  }
];
